home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Auge 4000 / Auge 4000 #17 (1988-04-01)(Amiga User Gruppe Einzugsgebiet 4000).zip / Auge 4000 #17 (1988-04-01)(Amiga User Gruppe Einzugsgebiet 4000).adf / DrunkenMouse / drunkenmouse.c < prev    next >
C/C++ Source or Header  |  1996-12-24  |  6KB  |  234 lines

  1. /* ===========================================================
  2.    ===                Drunken  Mouse                       ===
  3.    ===         By: Alex Livshits   July 1987               ===
  4.    =========================================================== */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <exec/interrupts.h>
  9. #include <graphics/gels.h>
  10. #include <graphics/sprite.h>
  11. #include <graphics/text.h>
  12. #include <intuition/intuition.h>
  13. #include <devices/input.h>
  14. #include <devices/inputevent.h>
  15.  
  16.  
  17.  
  18. #define IMAGE_HEIGHT (POINTERSIZE/2-2)
  19.  
  20.  
  21. #define INPUT    0x00000001
  22. #define VBLANK   0x00000002
  23. /* =====  EXPORT ======= */
  24. APTR MyTask;
  25. ULONG INPUTEVENT;
  26. SHORT PosX,PosY,DY;
  27. /***/
  28.  
  29. /* =====  IMPORT ======= */
  30. APTR IntuitionBase;
  31. APTR GfxBase;
  32. extern int in_start(),in_end(),in_moveptr();
  33. extern int vblank_start(),vblank_end();
  34. extern ULONG VBEVENT;
  35. /***/
  36.  
  37. static ULONG INTUIEVENT;
  38. static LONG signal;
  39. static struct Window *win;
  40. static struct RastPort *rport;
  41. static struct ViewPort *vport;
  42. static struct SimpleSprite Sprite1,Sprite2;
  43. static int sprnum = -1;
  44.  
  45. static struct Preferences *Pref,*Pref1;
  46. static SHORT STOPPGM;
  47. static ULONG mask = 0;
  48.  
  49.  
  50. static
  51. struct NewWindow NewWindow = {
  52.    120,0,   /* window XY origin relative to TopLeft of screen */
  53.    250,10,   /* window width and height */
  54.    0,1,   /* detail and block pens */
  55.    CLOSEWINDOW,   /* IDCMP flags */
  56.    ACTIVATE+WINDOWDEPTH+WINDOWDRAG+
  57.    WINDOWCLOSE, /* other window flags */
  58.    NULL,   /* first gadget in gadget list */
  59.    NULL,   /* custom CHECKMARK imagery */
  60.    " Drunken Mouse ",   /* window title */
  61.    NULL,   /* custom screen */
  62.    NULL,   /* custom bitmap */
  63.    0,0,   /* minimum width and height */
  64.    0,0,   /* maximum width and height */
  65.    WBENCHSCREEN   /* destination screen type */
  66. };
  67.  
  68. static SHORT delta1=0;
  69. static SHORT inc = -2;
  70. static SHORT delta2 = -10;
  71. static SHORT inc2=2;
  72.  
  73. void
  74. main()
  75. {
  76.    struct IntuiMessage *imsg;
  77.    ULONG  class,event;
  78.    USHORT code;
  79.    struct Gadget *gad;
  80.  
  81.    GfxBase = OpenLibrary("graphics.library",0);
  82.    IntuitionBase = OpenLibrary("intuition.library",0);
  83.  
  84.    win = OpenWindow(&NewWindow);
  85.    if (!win) { DisplayBeep(0); cleanup(); }
  86.  
  87.    rport = win->RPort;
  88.    INTUIEVENT = 1L<<win->UserPort->mp_SigBit;
  89.    vport = ViewPortAddress(win);
  90.  
  91.    MyTask = FindTask(0);
  92.    SetTaskPri(MyTask,-10);
  93.    signal = AllocSignal(-1);
  94.    if (signal==-1) { printf("\nAllocSignal failed."); cleanup(); }
  95.    INPUTEVENT = 1L<<signal;
  96.  
  97.  
  98.    if (in_start()) { printf("\nin_start() failed."); cleanup();}
  99.    mask |= INPUT;
  100.  
  101.    if (vblank_start()) { printf("\nvblank_start() failed."); cleanup();}
  102.    mask |= VBLANK;
  103.  
  104.    sprnum = GetSprite(&Sprite1,-1);
  105.    if (sprnum==-1) { printf("\n No more sprites"); cleanup(); }
  106.  
  107.    sprnum = GetSprite(&Sprite2,-1);
  108.    if (sprnum==-1) { printf("\n No more sprites"); cleanup(); }
  109.  
  110.    Sprite1.x = 0;
  111.    Sprite1.y = 0;
  112.    Sprite1.height = IMAGE_HEIGHT;
  113.  
  114.    Sprite2.x = 0;
  115.    Sprite2.y = 0;
  116.    Sprite2.height = IMAGE_HEIGHT;
  117.  
  118.  
  119.    Pref = AllocMem(sizeof(struct Preferences),MEMF_CHIP);
  120.    if (!Pref)
  121.    {
  122.       cleanup();
  123.    }
  124.  
  125.    Pref1 = AllocMem(sizeof(struct Preferences),MEMF_CHIP);
  126.    if (!Pref1)
  127.    {
  128.       cleanup();
  129.    }
  130.  
  131.    GetPrefs(Pref,sizeof(struct Preferences));
  132.  
  133.    sprnum = (sprnum & 0x06)*2+16;
  134.    SetRGB4(vport,sprnum+1,(Pref->color17)>>8,((Pref->color17)>>4)&0x0F,
  135.                           (Pref->color17)&0x0F);
  136.    SetRGB4(vport,sprnum+2,(Pref->color18)>>8,((Pref->color18)>>4)&0x0F,
  137.                           (Pref->color18)&0x0F);
  138.    SetRGB4(vport,sprnum+3,(Pref->color19)>>8,((Pref->color19)>>4)&0x0F,
  139.                           (Pref->color19)&0x0F);
  140.  
  141.    ChangeSprite(vport,&Sprite1,Pref->PointerMatrix);
  142.    GetPrefs(Pref1,sizeof(struct Preferences));
  143.    ChangeSprite(vport,&Sprite2,Pref1->PointerMatrix);
  144.  
  145.  
  146.    PosSprite(&Sprite1,0,0);
  147.    PosSprite(&Sprite2,0,0);
  148.    in_moveptr(0,0);
  149.  
  150.    SetWindowTitles(win,-1," Drunken Mouse (C)FLam ==A.Livshits & J-M.Forgeas==");
  151.    PosX=PosY=0;
  152.    STOPPGM = FALSE;
  153.  
  154.  
  155.    while (!STOPPGM)
  156.    {
  157.       event = Wait(INTUIEVENT|INPUTEVENT|VBEVENT);
  158.       if (event & INTUIEVENT)
  159.       {
  160.          while (imsg=(struct IntuiMessage *)GetMsg(win->UserPort))
  161.          {
  162.             class = imsg->Class;
  163.             code = imsg->Code;
  164.             gad = imsg->IAddress;
  165.             ReplyMsg(imsg);
  166.             switch(class)
  167.             {
  168.                case CLOSEWINDOW:
  169.                   STOPPGM=TRUE;
  170.                   break;
  171.  
  172.                default:
  173.                   break;
  174.             }
  175.          }
  176.       } /* end if INTUIEVENT */
  177.  
  178.       if (event & INPUTEVENT)
  179.       {
  180.  
  181.          if ((delta1<-30)||(delta1>30)) inc=-inc;
  182.          delta1 += inc;
  183.          PosSprite(&Sprite1,PosX+delta1-4,PosY-delta1-4);
  184.          PosSprite(&Sprite2,PosX-delta1,PosY+delta1);
  185.  
  186.       }
  187.  
  188.       else if (event & VBEVENT)
  189.       {
  190.          if ((delta1<-30)||(delta1>30)) inc=-inc;
  191.          delta1 += inc;
  192.          WaitBOVP(vport);
  193.          PosSprite(&Sprite1,PosX+delta1-4,PosY-delta1-4);
  194.          PosSprite(&Sprite2,PosX-delta1,PosY+delta1);
  195.  
  196.       }
  197.  
  198.    }
  199.  
  200.    cleanup();
  201. }
  202.  
  203. static
  204. PosSprite(sprite,x,y)
  205.    ULONG *sprite;
  206.    SHORT x,y;
  207. {
  208.    x = (x>>1)+Pref->XOffset;
  209.    y = (y>>1)+Pref->YOffset;
  210.    MoveSprite(0,sprite,x,y);
  211. }
  212.  
  213.  
  214. static
  215. cleanup()
  216. {
  217.    if (mask & VBLANK) vblank_end();
  218.    if (mask & INPUT) in_end();
  219.    if (sprnum != -1) {
  220.         FreeSprite(Sprite1.num);
  221.         FreeSprite(Sprite2.num);
  222.    }
  223.    if (win) CloseWindow(win);
  224.    if (Pref) FreeMem(Pref,sizeof(*Pref));
  225.    if (Pref1) FreeMem(Pref1,sizeof(*Pref1));
  226.  
  227.    if (signal != -1) FreeSignal(signal);
  228.    CloseLibrary(IntuitionBase);
  229.    CloseLibrary(GfxBase);
  230.  
  231.    exit(0);
  232. }
  233.  
  234.